package org.dromara.system.utils;

import java.util.regex.Pattern;

public class IPv4Validator {
    private static final String IPV4_REGEX =
        "^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." +
            "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." +
            "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." +
            "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";

    private static final Pattern IPV4_PATTERN = Pattern.compile(IPV4_REGEX);

    public static boolean isValidIPv4(String ip) {
        if (ip == null || ip.isEmpty()) {
            return false;
        }
        return IPV4_PATTERN.matcher(ip).matches();
    }

    public static void main(String[] args) {
        String validIPv4 = "192.168.0.1";
        String invalidIPv4 = "256.0.0.1";
        String emptyString = "";
        String nullString = null;

        System.out.println(validIPv4 + " is valid IPv4: " + isValidIPv4(validIPv4));
        System.out.println(invalidIPv4 + " is valid IPv4: " + isValidIPv4(invalidIPv4));
        System.out.println(emptyString + " is valid IPv4: " + isValidIPv4(emptyString));
        System.out.println(nullString + " is valid IPv4: " + isValidIPv4(nullString));
    }
}
